package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.domain.*;
import com.woniuxy.dto.*;
import com.woniuxy.parameter.CommentParam;
import com.woniuxy.parameter.DealTimeParam;
import com.woniuxy.parameter.PageParam;
import com.woniuxy.parameter.QueryBugParam;
import com.woniuxy.service.*;
import com.woniuxy.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName BugDistributionController
 * @Description 缺陷处理任务分配以及反馈
 * @Author 李磊
 * @Version 1.0
 **/
@RestController
@RequestMapping("/bugDistribution")
@CrossOrigin
@Api(tags = "分配缺陷")
public class BugDistributionController {
    /*工时业务层注入*/
    @Autowired
    private DealtimeService dealtimeService;
    /*缺陷记录业务层注入*/
    @Autowired
    private RecordbugService recordbugService;
    /*缺陷分类业务层注入*/
    @Autowired
    private BugclassifyService bugclassifyService;
    /*缺陷状态业务层注入*/
    @Autowired
    private BugstateService bugstateService;
    /*缺陷严重程度业务层注入*/
    @Autowired
    private BuglevelService buglevelService;
    /*用户业务层注入*/
    @Autowired
    private UserService userService;
    /*评论业务层注入*/
    @Autowired
    private CommentService commentService;
    /*项目表业务层注入*/
    @Autowired
    private ProjectService projectService;


    /**
     *@Author 李磊
     *@Description 新增工时表（分配缺陷任务）
     *@Param [parmDealtime]
     *@return com.woniuxy.dto.JSONResult
     **/
    @PostMapping
    @ApiOperation("新增工时信息")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult deal(DealTimeParam dealTimeParam) throws Exception {
        Dealtime dealtime = new Dealtime();
        BeanUtils.copyProperties(dealTimeParam,dealtime);
        dealtimeService.save(dealtime);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,null);
    }

    /**
     *@Author 李磊
     *@Description 查询缺陷处理进度
     *@Param [bugId]
     *@return com.woniuxy.dto.JSONResult
     **/
    @GetMapping("/queryDeal")
    @ApiOperation("查询缺陷处理进度")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult queryDeal(Integer bugId){

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("bug_id",bugId);
        Dealtime dealtime = dealtimeService.getOne(wrapper);
        DtoDealtime dtoDealtime = new DtoDealtime();
        BeanUtils.copyProperties(dealtime,dtoDealtime);

        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), dtoDealtime,null);
    }
    /**
     *@Author 李磊
     *@Description 修改工时信息做反馈
     *@Param [dealTimeParam]
     *@return com.woniuxy.dto.DtoJSONResult
     **/
    @PostMapping("/updateDeal")
    @ApiOperation("修改工时信息做反馈")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult updateDeal(DealTimeParam dealTimeParam){
        Dealtime dealtime = new Dealtime();
        BeanUtils.copyProperties(dealTimeParam,dealtime);
        dealtimeService.saveOrUpdate(dealtime);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,null);
    }

    /**
     *@Author 李磊
     *@Description 查询当前项目缺陷信息
     *@Param [id]项目缺陷编号
     *@return com.woniuxy.dto.JSONResult
     **/
    @GetMapping("/queryBug")
    @ApiOperation("查询当前项目缺陷信息")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult queryBug(int id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("bug_id",id);
        Recordbug recordbug = recordbugService.getOne(wrapper);
        DtoRecordbug dtoRecordbug = new DtoRecordbug();
        BeanUtils.copyProperties(recordbug,dtoRecordbug);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), dtoRecordbug, null);
    }

    /**
     *@Author 李磊
     *@Description 查询可分配任务的成员
     *@Param [id]
     *@return com.woniuxy.dto.DtoJSONResult
     **/
    @GetMapping("/queryUser")
    @ApiOperation("查询可分配任务的成员")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult queryUser(Integer id){

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id",id);

        List<User> list = userService.list(wrapper);
        List<String> names = new ArrayList<>();
        for(int i = 0;i < list.size();i ++){
            String userName = list.get(i).getUserName();
            names.add(userName);
        }
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null, names);

    }

    /**
     *@Author 李磊
     *@Description 统计缺陷数据分类占比
     *@Param [localDateTime1, localDateTime2]
     *@return com.woniuxy.dto.JSONResult
     **/
    @PostMapping("/statistical")
    @ApiOperation("统计缺陷数据分类占比")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult statistical(String dateParam1, String dateParam2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DtoFormatParam.FORMAT);
        Date date1 = sdf.parse(dateParam1);
        Date date2 = sdf.parse(dateParam2);
        //查询缺陷分类表格中的所有bug分类
        List<Bugclassify> list = bugclassifyService.list();
        double all=0;
        List<DtoBugclassify> list2=new ArrayList<DtoBugclassify>();
        for (Bugclassify bugclassify : list) {
            String classifyDetail = bugclassify.getClassifyDetail();
            List<Recordbug> recordbugs = recordbugService.queryRecordbug(classifyDetail, date1, date2);
            all += recordbugs.size();
        }
        //根据不同分类分别查询选定时间段的数据
        for(int i = 0;i < list.size();i ++){
            Bugclassify bugclassify = list.get(i);
            //key为分类名
            String classifyDetail = bugclassify.getClassifyDetail();
            //value为当前分类数据数量
            List<Recordbug> recordbugs = recordbugService.queryRecordbug(classifyDetail, date1, date2);
            double size = recordbugs.size();
            DtoBugclassify dtoBugclassify = new DtoBugclassify();
            dtoBugclassify.setClassifyDetail(classifyDetail);
            dtoBugclassify.setSize( size/all);
            System.out.println(dtoBugclassify.getSize());
            list2.add(dtoBugclassify);
        }
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,list2);
    }
    /**
     *@Author 李磊
     *@Description 根据状态查询bug记录
     *@Param [state]
     *@return com.woniuxy.dto.DtoJSONResult
     **/
    @GetMapping("/queryByState")
    @ApiOperation("根据状态查询bug记录")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult queryByState(QueryBugParam queryBugParam, PageParam pages){

        PageInfo pageInfo = recordbugService.queryBugByNameOrState(queryBugParam, pages);

        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), pageInfo,null);
    }

    /**
     *@Author 李磊
     *@Description 统计缺陷数据状态占比
     *@Param [localDateTime1, localDateTime2]
     *@return com.woniuxy.dto.JSONResult
     **/
    @PostMapping("/stateStatistical")
    @ApiOperation("统计缺陷数据状态占比")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult stateStatistical(String dateParam1, String dateParam2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DtoFormatParam.FORMAT);
        Date date1 = sdf.parse(dateParam1);
        Date date2 = sdf.parse(dateParam2);
        //查询缺陷状态表格中的所有bug分类
        List<Bugstate> list = bugstateService.list();
        //集合保存统计数据返回前端
        ArrayList<DtoBugstate> dtoBugstates = new ArrayList<>();
        //根据不同状态分别查询选定时间段的数据
        for(int i = 0;i < list.size();i ++){
            Bugstate bugstate = list.get(i);
            //key为状态名
            String dostate = bugstate.getBugstateDetail();
            //value为当前状态数据数量
            List<Recordbug> dostates = recordbugService.queryRecordbugState(dostate, date1, date2);
            Integer size = dostates.size();
            DtoBugstate dtoBugstate = new DtoBugstate();
            dtoBugstate.setBugstateDetail(dostate);
            dtoBugstate.setSize(size);
            dtoBugstates.add(dtoBugstate);
        }
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,dtoBugstates);
    }

    /**
     *@Author 李磊
     *@Description 统计缺陷数据严重程度占比
     *@Param [localDateTime1, localDateTime2]
     *@return com.woniuxy.dto.JSONResult
     **/
    @PostMapping("/levelStatistical")
    @ApiOperation("统计缺陷数据严重程度占比")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult levelStatistical(String dateParam1, String dateParam2) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(DtoFormatParam.FORMAT);
        Date date1 = sdf.parse(dateParam1);
        Date date2 = sdf.parse(dateParam2);

        //查询缺陷严重程度表格中的所有bug分类
        List<Buglevel> list = buglevelService.list();
        //键值对保存统计数据返回前端
        HashMap hashMap = new HashMap();
        //根据不同严重程度分别查询选定时间段的数据
        for(int i = 0;i < list.size();i ++){
            Buglevel buglevel = list.get(i);
            //key为分类名
            String level = buglevel.getBuglevelDetail();
            //value为当前严重程度数据数量
            List<Recordbug> levels = recordbugService.queryRecordbugDegree(level, date1, date2);
            int size = levels.size();
            hashMap.put(level,size);
        }
        Object[] objects = hashMap.entrySet().toArray();
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), objects,null);
    }
    /**
    *@Author 李磊
    *@Description 新增评论
    *@Param [commentParam]
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @PostMapping("/comment")
    @ApiOperation("新增评论")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult insertComment(CommentParam commentParam){
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentParam,comment);
        commentService.save(comment);
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,null);
    }
    /**
    *@Author 李磊
    *@Description 查询所有项目名
    *@Param []
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @GetMapping("/queryAllPN")
    @ApiOperation("查询所有项目名")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult queryAllProjectName(){

        List<Project> list = projectService.list();
        List<String> nameList = new ArrayList<>();

        for (int i = 0;i <list.size();i ++){
            nameList.add(list.get(i).getProjectName());
        }

        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,nameList);
    }
    /**
    *@Author 李磊
    *@Description 查询所有状态
    *@Param []
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @GetMapping("/queryAllState")
    @ApiOperation("查询所有状态")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult queryAllState(){

        List<Bugstate> list = bugstateService.list();

        List<String> stateList = new ArrayList<>();

        for (int i = 0;i <list.size();i ++){
            stateList.add(list.get(i).getBugstateDetail());
        }
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,stateList);
    }
    /**
    *@Author 李磊
    *@Description 修改时工表状态
    *@Param [dealTimeParam]
    *@return com.woniuxy.dto.DtoJSONResult
    **/
    @PostMapping("/updateDealTime")
    @ApiOperation("修改时工表状态")
    @RequiresPermissions({"缺陷管理"})
    public DtoJSONResult updateDealTime(DealTimeParam dealTimeParam){
        Dealtime dealtime = new Dealtime();
        BeanUtils.copyProperties(dealTimeParam,dealtime);
        dealtimeService.saveOrUpdate(dealtime);

        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(), null,null);
    }


}
